package com.gitee.ed.structure.map;

import cn.hutool.core.getter.OptNullBasicTypeFromObjectGetter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 以 String 作为唯一键的特殊 ConcurrentHashMap
 * 该 Map 提供基础的类型转换能力
 * 以及当类型转换失败时，自动返回默认值的能力
 *
 * @author hongda.li
 */
public class StringConcurrentMap<V> extends ConcurrentHashMap<String, V> implements OptNullBasicTypeFromObjectGetter<String> {

    public StringConcurrentMap() {
        this(8);
    }

    public StringConcurrentMap(int initialCapacity) {
        super((int) (initialCapacity / 0.75F) + 1);
    }

    public StringConcurrentMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public StringConcurrentMap(Map<String, V> otherMap) {
        super(otherMap);
    }

    @Override
    public Object getObj(String key, Object defaultValue) {
        V value = this.get(key);
        return value == null ? defaultValue : value;
    }

    /**
     * 链式调用 put 方法
     *
     * @param key   键
     * @param value 值
     * @return 链式调用
     */
    public StringConcurrentMap<V> chainPut(String key, V value) {
        this.put(key, value);
        return this;
    }
}
